package 热题100;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

//滑动窗口的思路
public class Num3_lengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String s) {
        int length = s.length();
        int max = 0;

        Map<Character, Integer> map = new HashMap<>();
        for(int start = 0, end = 0; end < length; end++){
            char element = s.charAt(end);
            if(map.containsKey(element)){
                start = Math.max(map.get(element) + 1, start); //map.get()的地方进行+1操作
            }
            max = Math.max(max, end - start + 1);
            map.put(element, end);
        }
        return max;
    }

    public static void main(String[] args) {
        Num3_lengthOfLongestSubstring num3 = new Num3_lengthOfLongestSubstring();
        num3.lengthOfLongestSubstring("acbcdase");
    }
}



//3. 无重复字符的最长子串
/*public class Num3_lengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String s) {

        int max = 0;
        int t = 0;//记录读取字符串的位置
        while(t < s.length()){
            HashSet<Character> set = new HashSet<>();
            int conut = 0;
            int tmp = t;//保存t进入前的位置
            while( t < s.length() && set.add(s.charAt(t)) ){
                t++;
                conut ++;
            }
            max = max > conut ? max : conut;
            t = tmp + 1;
        }
        return max;
    }

    public static void main(String[] args) {
        Num3_lengthOfLongestSubstring n3 = new Num3_lengthOfLongestSubstring();
        int xx = n3.lengthOfLongestSubstring("dvdf");
        System.out.println(xx);
    }
}*/
